package com.example.demo.model.page;

import io.swagger.annotations.ApiModelProperty;
import lombok.Setter;

import java.io.Serializable;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

/**
 * 排序
 *
 * @author Liunh
 */
@Setter
public class SortReq implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * key:排序字段
     * value:排序方式
     */
    @ApiModelProperty(name = "sort", value = "排序条件")
    private Map<String, SortType> sort;

    public Map<String, SortType> getSort() {
        return sort;
    }

    public static enum SortType {

        /**
         * 正序
         */
        ASC,

        /**
         * 倒叙
         */
        DESC;

        private SortType() {
        }

        public static SortType fromString(String value) {
            try {
                return valueOf(value.toUpperCase(Locale.US));
            } catch (Exception var2) {
                throw new IllegalArgumentException(String.format("Invalid value '%s' for orders given; Has to be either 'desc' or 'asc' (case insensitive)", value), var2);
            }
        }

        public static Optional<SortType> fromOptionalString(String value) {
            try {
                return Optional.ofNullable(fromString(value));
            } catch (IllegalArgumentException var2) {
                return Optional.empty();
            }
        }

        public boolean isAscending() {
            return this.equals(ASC);
        }

        public boolean isDescending() {
            return this.equals(DESC);
        }
    }
}
